﻿using System;
using System.Collections.Generic;
using System.Linq;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Models.Movies;

namespace Rodunu.Business.Logic
{
    public class CollectionBL : ICollection
    {
        private readonly IManageSettings _manageSettings;

        #region ICollection Members
        
        public CollectionBL(IManageSettings manageSettings)
        {
            _manageSettings = manageSettings;
        }

        public void Save(IList<Title> myTitles)
        {
            _manageSettings.CurrentUser.Collection = myTitles;
            _manageSettings.SaveCurrentSettings();
        }

        public IList<Title> GetMyTitles()
        {
            if (_manageSettings.CurrentUser != null)
                return _manageSettings.CurrentUser.Collection;
            else
                return new List<Title>();
        }

        public IList<Title> SearchTitles(string searchFor)
        {
            if (_manageSettings.CurrentUser != null && _manageSettings.CurrentUser.Collection != null)
            {
                IList<Title> myTitles = _manageSettings.CurrentUser.Collection;
                return (from title in myTitles
                        where title.Name.ToLower().Contains(searchFor.ToLower()) 
                        || searchFor.ToLower().Contains(title.Name.ToLower())
                        || title.Name.ToLower() == searchFor.ToLower()
                        select title).ToList();
            }
            return null;
        }

        #endregion
    }
}
